
import { _decorator, Component, Node, CircleCollider2D, ERigidBody2DType, RigidBody2D, v2, BoxCollider2D, size, Enum } from 'cc';
import LevelColler from '../../MainScene/Script/LevelColler';
import GameConfig from '../../Script/GameSpecial/GameConfig';
import { EventType } from '../../Script/GameSpecial/GameEventType';
import { GlobalEnum } from '../../Script/GameSpecial/GlobalEnum';
const { ccclass, property } = _decorator;

/**可破坏的障碍物 */
@ccclass('Barrier')
export class Barrier extends LevelColler {

    public beHit(role: LevelColler) {
        //爆出金币
        this.emit(EventType.LevelEvent.dropGold, { p: this.node.position, count: Math.round(Math.random() * 1) + 2 });
        //添加破坏动画
        if (!!this.breakEffectName) {
            this.emit(EventType.LevelEvent.addBreakAnim, {
                p: this.node.position,
                breakEffectName: this.breakEffectName,
            });
        }
        //移除障碍物
        switch (this.barrierType) {
            case GlobalEnum.BarrierType.breakBarrier: {
                this.emit(EventType.LevelEvent.removeBreakBarrier, this);
                break;
            }
            case GlobalEnum.BarrierType.dynamicBarrier: {
                this.emit(EventType.LevelEvent.removeDynamicBarrier, this);
                break;
            }
        }
    }

    @property({
        type: Enum(GlobalEnum.BarrierType)
    })
    protected barrierType: GlobalEnum.BarrierType = GlobalEnum.BarrierType.breakBarrier;

    /**被击破坏时要播放的动画预制件名称 */
    @property
    protected breakEffectName: string = "";

    @property({
        tooltip: "被破坏音效，0：木材，1：石头，2：车辆"
    })
    public breakAudioType: number = 0;

    protected createRigidNode() {
        if (!!this.rigidNode) return;
        //碰撞体节点
        let node = new Node(GlobalEnum.RigidNode.role);
        this.rigidNode = node;
        //刚体组件
        let rigid = node.addComponent(RigidBody2D);
        rigid.group = GlobalEnum.RigidGroup.default;
        rigid.enabledContactListener = false;
        rigid.bullet = false;
        if (this.barrierType == GlobalEnum.BarrierType.dynamicBarrier) {
            rigid.type = ERigidBody2DType.Dynamic;
        } else {
            rigid.type = ERigidBody2DType.Static;
        }
        rigid.allowSleep = true;
        rigid.gravityScale = 0;
        rigid.linearDamping = 0.5;
        rigid.angularDamping = 0.5;
        rigid.linearVelocity = v2(0, 0);
        rigid.angularVelocity = 0;
        rigid.fixedRotation = false;
        rigid.awakeOnLoad = false;
        this.rigid = rigid;
        //碰撞体形状
        let collider = node.addComponent(BoxCollider2D);
        collider.tag = 0;
        collider.group = GlobalEnum.RigidGroup.default;
        collider.density = 1;
        collider.sensor = false;
        collider.friction = 0;
        collider.restitution = 0;
        collider.offset = v2(0, 0);
        let w = this.maxVert.x - this.minVert.x;
        let h = this.maxVert.z - this.minVert.z;
        let r = GameConfig.GameRule.rate3to2;
        collider.size = size(w * r, h * r);
        this.collider2d = collider;
    }

}

